package definite.Guide.Book.JColorChooser;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class CustomJColorChooser {

	public static void main(String[] args) {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("Customize JColor Chooser");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				final JButton button = new JButton("Pick to change background");
				ActionListener action = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						Color init = button.getBackground();
						final JColorChooser clChoose = new JColorChooser(init);

						// register for ok button in dialog
						ActionListener okAction = new ActionListener() {

							@Override
							public void actionPerformed(ActionEvent e) {
								Color newColor = clChoose.getColor();
								if (newColor.equals(button.getForeground()))
									JOptionPane.showMessageDialog(null,
											"Color changed rejected",
											"Cannot change color",
											JOptionPane.ERROR_MESSAGE);
								else
									button.setBackground(newColor);
							}
						}; // end of OK

						// register for cancel button in dialog
						ActionListener cancelAction = new ActionListener() {

							@Override
							public void actionPerformed(ActionEvent e) {
								button.setBackground(Color.RED);
							}
						}; // end cancel

						final JDialog dialog =
							JColorChooser.createDialog(null,
									"Change Button BackGround", true, clChoose,
									okAction, cancelAction);

						// ********* CUSTOMIZE THE PREVIEW PANEL*************//

						// if dont't want the preView Panel
						// clChoose.setPreviewPanel(new JPanel());

						// custom it
						final JLabel previewLabel =
							new JLabel("I Love Swing", JLabel.CENTER);
						previewLabel.setFont(new Font("Serif", Font.BOLD
							| Font.ITALIC, 48));
						previewLabel.setSize(previewLabel.getPreferredSize());
						previewLabel.setBorder(BorderFactory.createEmptyBorder(
								0, 0, 1, 0));
						clChoose.setPreviewPanel(previewLabel);

						/*
						 If you don’t like the default chooser panels, or you just want to add other color chooser panels 
						that work differently, you can create your own by subclassing the AbstractColorChooserPanel
						class. To add a new panel to the existing set, call the following method:
						
						public void addChooserPanel(AbstractColorChooserPanel panel)
						
						If you later decide that you no longer want the new panel, you can remove it with this method:
						public AbstractColorChooserPanel removeChooserPanel(AbstractColorChooserPanel panel)
						To replace the existing set ofpanels, call this method:
						
						setChooserPanels(AbstractColorChooserPanel panels[ ])
						
						Creating a new panel entails subclassing AbstractColorChooserPaneland filling in the 
						details of choosing a color for the new panel. The class definition, shown in the following code 
						lines, includes five abstract methods. These five methods are what must be overridden.
						
						
						public abstract class AbstractColorChooserPanel extends JPanel {
						public AbstractColorChooserPanel();
						protected abstract void buildChooser();
						protected Color getColorFromModel();
						public ColorSelectionModel getColorSelectionModel();
						public int getDisplayMnemonicIndex();
						public abstract String getDisplayName();
						public abstract Icon getLargeDisplayIcon();
						public int getMnemonic();
						public abstract Icon getSmallDisplayIcon();
						public void installChooserPanel(JColorChooser);
						public void paint(Graphics);
						public void uninstallChooserPanel(JColorChooser);
						public abstract void updateChooser();
						
						}
						
						*/
						// *********** END CUSTOMIZE PREVIEW****************//

						// wait for current event dispatching to complete before
						// showing
						Runnable showDialog = new Runnable() {

							@Override
							public void run() {
								dialog.setVisible(true);
							}
						};
						EventQueue.invokeLater(showDialog);
					}
				};
				button.addActionListener(action);

				frame.add(button, BorderLayout.CENTER);
				frame.setSize(400, 200);
				frame.setLocationRelativeTo(null);
				frame.setResizable(false);
				frame.setVisible(true);
			}

		};

		EventQueue.invokeLater(r);
	}

}
